﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using SupportLibrary.WebMethod;
using Automation.Protocols.Model;
using SupportLibrary.Message;

using Common.Logging;

namespace Driver.Features
{
    public class BuildGuiMap:ConsoleBase
    {
        private ILog log = LogManager.GetLogger(typeof(BuildRelease));
        private IDataAccess _dtAccess = null;
        private Dictionary<String, String> _tConfigs = null;
        private String _action = String.Empty;

        //generate gui map
        private List<String> _pageList;
        private List<String> _tagList;

        public BuildGuiMap(IDataAccess dtAccess, string action )
        {
            _dtAccess = dtAccess;
            _action = action;
        }

        protected override bool Input()
        {
            bool success = false;
            try {
                _tConfigs = _dtAccess.getTestConfig();
                string pages = _tConfigs["pageList"];
                string tags = _tConfigs["tagList"];
                _pageList = WebResource.Attributes(pages);
                _tagList = WebResource.Attributes(tags);

                success = true;
            }
            catch (Exception e)
            {
                log.Debug(Resources.Msg.BuildGuiMap_Input_Error + e.Message);
            }
            return success;
        }

        protected override bool Validator()
        {
            bool success = false;
            try {
                switch (_action)
                {
                    case "buildresource":
                        if (_pageList.Count > 0 && _tagList.Count > 0)
                            success = true;
                        else
                            log.Debug(Resources.Msg.TConfig_DefaultValue_err);

                        break;
                    default:
                        if (_pageList.Count > 0 && _tagList.Count > 0)
                            success = true;
                        else
                            log.Debug(Resources.Msg.TConfig_DefaultValue_err);
                        break;
                }
            }
            catch (Exception e)
            {
                log.Debug(Resources.Msg.BuildGuiMap_Validator_Err);
            }
            return success;
        }
        private bool CrawlGui()
        {
            bool success = false;
            int id = 0;
            IWebmethod webMethod = new SeleniumWebmethod("FireFox");

            Dictionary<String, String> tagAttributes = WebResource.Tags();

            List<TestMap> listTMap = new List<TestMap>();

            try
            {
                do
                {
                    string action = Console.ReadLine();
                    if (action.Equals("/e"))
                    {
                        break;
                    }
                    else
                    {

                        foreach (String tag in _tagList)
                        {

                            List<String> listAttribute = webMethod.GetListElementAttributeAtCurrentPage(tag);
                            foreach (String attribute in listAttribute)
                            {
                                id++;
                                TestMap tMap = new TestMap();
                                tMap.ID = id.ToString();
                                tMap.Page = webMethod.CurrentUrl();
                                tMap.TagName = tag;
                                tMap.AttributeValue = attribute;
                                tMap.ControlName = tag + "_" + id.ToString();
                                tMap.Xpath = String.Empty;
                                listTMap.Add(tMap);
                            }
                        }
                    }
                } while (true);
                _dtAccess.InsertTMaps(listTMap);
                success = true;
            }
            catch (Exception e)
            {
                log.Debug(Resources.Msg.BuildGuiMap_Run_Err);
            }
            return success;
        }

        private bool BuildResource()
        {
            bool success = false;
            return success;
        }

        protected override bool Run()
        {
            bool success = false;
            try
            {
                switch (_action)
                {
                    case "buildresource":
                        success = BuildResource();

                        break;
                    default:
                        success = CrawlGui();
                        break;
                }
            }
            catch (Exception e)
            {
                log.Debug(Resources.Msg.BuildGuiMap_Validator_Err + e.Message);
            }
            return success;
        }
    }
}
